xquery version "1.0";

(: filename w_13 :)
import module namespace xsc = "http://localhost.nrg/"
at "/home/nvno/www/xscrawler/xquery-lib/util.xq" ;

declare variable $mapping := doc( '/home/nvno/www/xscrawler/mapping.xml' );

<ZZMatches week="13">
{
	let $zzXml := doc( '/home/nvno//www/xscrawler/cache/zzxml/zerozero_games.xml' )
	for $game in $zzXml//game
	return	
		<game>
			<zz_id>{ data($game/@id) }</zz_id>
			<date>{ normalize-space($game/td//div[@class = 'fj_header_datatv']/text()[1]) }</date>
			<stadium>{ data($game//div[@class = 'fj_header_estadioarbitro']/a[1]/text()) }</stadium>
			<referee>{ data($game/td//div[@class = 'fj_header_estadioarbitro']/a[2]/text()) }</referee>
			{ 
				let $result := tokenize(data($game/td//td[@class = 'fj_marcador_txt']/text()),':')
				return
					<score>
					<home>{ $result[position() = 1] }</home>
					<visitor>{ $result[position() = 2] }</visitor>
								</score>
			 }	
			<home_team>
				<zz_id>{ xsc:get-zzteam-id(data($game/td//div[@class = 'fj_equipa_casa']/a/@href)) }</zz_id>
				<zz_name>{ normalize-space($game/td//div[@class = 'fj_equipa_casa']/a/text()[1]) }</zz_name>
				<xs_id>{ $mapping/*//*:team[./*:zz = normalize-space($game/td//div[@class = 'fj_equipa_casa']/a/text()[1])]/*:xs/@*:id }</xs_id>
				<xs_name>{ $mapping/*//*:team[./*:zz = normalize-space($game/td//div[@class = 'fj_equipa_casa']/a/text()[1])]/*:xs/text() }</xs_name>
				<lineup>
				{
					for $player in $game/home_team/td[@class = 'fj_ficha_equipa_casa']/table/tbody/tr
					return 
						<player>
							<name>{ $player//td[@class='fj_ficha_nome']/a[2]/text() }</name>
							<number>{ $player//td[@class='fj_ficha_numero']/text() }</number>
							{ 
								if ( exists($player//td[@class='fj_ficha_subs']/div) ) then
									<substitution>
										<type>{ substring-after($player//td[@class='fj_ficha_subs']/div/@class, 'fj_ficha_') }</type>
										<minute>{ $player//td[@class='fj_ficha_subs']/div/text() }</minute>
									</substitution>
								else ()
							}
							{
								if( exists( $player//td[@class='fj_ficha_golo']) ) then
									<goals>
									{

										for $goal in tokenize( normalize-space(data($player//td[@class='fj_ficha_golo'])) , '\s')
										where contains( $goal, "'")
										return
											<minute>{ substring-before($goal, "'") }</minute>
									}
									</goals>
								else ()
							}
							{
								if( exists($player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']) ) then
									<discipline>
										{
										for $cards in $player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']
										return 
											for $card in tokenize($cards/text(),'\s')
											where contains($card,"'")
											return
												<card>
													<type>{ substring-after(data($cards/@class), 'fj_ficha_') }</type>
													<minute>{ substring-before($card,"'") }</minute>											
												</card>											
										}
									</discipline>
								else ()
							}
						</player>
				}
				</lineup>
				<substitutes>
					{
					for $player in $game/home_team/td[@class = 'fj_ficha_equipa_casa_suplente']/table/tbody/tr
					return 
						<player>
							<name>{ $player//td[@class='fj_ficha_nome']/a[2]/text() }</name>
							<number>{ $player//td[@class='fj_ficha_numero']/text() }</number>
							{ 
								if ( exists($player//td[@class='fj_ficha_subs']/div) ) then
									<substitution>
										<type>{ substring-after($player//td[@class='fj_ficha_subs']/div/@class, 'fj_ficha_') }</type>
										<minute>{ $player//td[@class='fj_ficha_subs']/div/text() }</minute>
									</substitution>
								else ()
							}
							{
								if( exists( $player//td[@class='fj_ficha_golo']) ) then
									<goals>
									{
										for $goal in tokenize( normalize-space(data($player//td[@class='fj_ficha_golo'])) , '\s')
										where contains( $goal, "'")
										return
											<minute>{ substring-before($goal, "'") }</minute>
									}
									</goals>
								else ()
							}
							{
								if( exists($player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']) ) then
									<discipline>
										{
										for $cards in $player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']
										return 
											for $card in tokenize($cards/text(),'\s')
											where contains($card,"'")
											return
												<card>
													<type>{ substring-after(data($cards/@class), 'fj_ficha_') }</type>
													<minute>{ substring-before($card,"'") }</minute>											
												</card>											
										}
									</discipline>
								else ()
							}
						</player>
				}
				</substitutes>
			</home_team>
			<visitor_team>
				<zz_id>{ xsc:get-zzteam-id(data($game/td//div[@class = 'fj_equipa_fora']/a/@href)) }</zz_id>
				<zz_name>{ normalize-space($game/td//div[@class = 'fj_equipa_fora']/a/text()[1]) }</zz_name>
				<xs_id>{ $mapping/*//*:team[./*:zz = normalize-space($game/td//div[@class = 'fj_equipa_fora']/a/text()[1])]/*:xs/@*:id }</xs_id>
				<xs_name>{ $mapping/*//*:team[./*:zz = normalize-space($game/td//div[@class = 'fj_equipa_fora']/a/text()[1])]/*:xs/text() }</xs_name>
				<lineup>
				{
					for $player in $game/away_team/td[@class = 'fj_ficha_equipa_fora']/table/tbody/tr
					return 
						<player>
							<name>{ $player//td[@class='fj_ficha_nome']/a[2]/text() }</name>
							<number>{ $player//td[@class='fj_ficha_numero']/text() }</number>
							{ 
								if ( exists($player//td[@class='fj_ficha_subs']/div) ) then
									<substitution>
										<type>{ substring-after($player//td[@class='fj_ficha_subs']/div/@class, 'fj_ficha_') }</type>
										<minute>{ $player//td[@class='fj_ficha_subs']/div/text() }</minute>
									</substitution>
								else ()
							}
							{
								if( exists( $player//td[@class='fj_ficha_golo']) ) then
									<goals>
									{
										for $goal in tokenize( normalize-space(data($player//td[@class='fj_ficha_golo'])) , '\s')
										return (<minute>{ $goal }</minute>)
									}
									</goals>
								else ()
							}
							{
								if( exists($player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']) ) then
									<discipline>
										{
										for $cards in $player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']
										return 
											for $card in tokenize($cards/text(),'\s')
											where contains($card,"'")
											return
												<card>
													<type>{ substring-after(data($cards/@class), 'fj_ficha_') }</type>
													<minute>{ substring-before($card,"'") }</minute>											
												</card>											
										}
									</discipline>
								else ()
							}
						</player>
				}
				</lineup>
				<substitutes>
					{
					for $player in $game/home_team/td[@class = 'fj_ficha_equipa_fora_suplente']/table/tbody/tr
					return 
						<player>
							<name>{ $player//td[@class='fj_ficha_nome']/a[2]/text() }</name>
							<number>{ $player//td[@class='fj_ficha_numero']/text() }</number>
							{ 
								if ( exists($player//td[@class='fj_ficha_subs']/div) ) then
									<substitution>
										<type>{ substring-after($player//td[@class='fj_ficha_subs']/div/@class, 'fj_ficha_') }</type>
										<minute>{ $player//td[@class='fj_ficha_subs']/div/text() }</minute>
									</substitution>
								else ()
							}
							{
								if( exists( $player//td[@class='fj_ficha_golo']) ) then
									<goals>
									{
										for $goal in tokenize( normalize-space(data($player//td[@class='fj_ficha_golo'])) , '\s')
										return (<minute>{ $goal }</minute>)
									}
									</goals>
								else ()
							}
							{
								if( exists($player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']) ) then
									<discipline>
										{
										for $cards in $player//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']
										return 
											for $card in tokenize($cards/text(),'\s')
											where contains($card,"'")
											return
												<card>
													<type>{ substring-after(data($cards/@class), 'fj_ficha_') }</type>
													<minute>{ substring-before($card,"'") }</minute>											
												</card>											
										}
									</discipline>
								else ()
							}
						</player>
				}
				</substitutes>
			</visitor_team>
		</game>
}
</ZZMatches>
